package com.bw.house.page

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.path.RouterPath
import com.bw.common.view.BaseMvvmActivity
import com.bw.house.R
import com.bw.house.adapter.HouseAreaAdapter
import com.bw.house.databinding.ActivityHouseBinding
import com.bw.house.entity.HouseAreaEntity
import com.bw.house.vm.HouseViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * 房源管理页面
 */
@Route(path = RouterPath.PATH_HOUSE_PATH)
@AndroidEntryPoint
class HouseActivity : BaseMvvmActivity<ActivityHouseBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_house

    private val mViewModel:HouseViewModel by viewModels()
    val houseAreaAdapter =HouseAreaAdapter()

    val houseAreaEntity = mutableListOf<HouseAreaEntity>()


    override fun initViews() {
        super.initViews()
        mDataBinding.rvHouse.adapter = houseAreaAdapter
        //点击区域  加载对应列表
        mDataBinding.rg1.setOnCheckedChangeListener { radioGroup, i ->
            when(i){
                R.id.radio_a->{
                    mViewModel.getHouseData(1)
                }
                R.id.radio_b->{
                    mViewModel.getHouseData(2)
                }
                R.id.radio_c->{
                    mViewModel.getHouseData(3)
                }
                R.id.radio_d->{
                    mViewModel.getHouseData(4)
                }
            }
        }

        mDataBinding.rg2.setOnCheckedChangeListener { radioGroup, i ->
            when(i){
                R.id.radio_one->{
                    mViewModel.mHouseSuccess.observe(this){
                        houseAreaAdapter.data.clear()
                        houseAreaEntity.clear()
                        val map = it.groupBy { it.area }
                        val list = map.keys.sorted()
                        var index = 1
                        for (key in list){
                            houseAreaEntity.add(HouseAreaEntity(index,key,map.get(key)!!))
                            index++
                        }
                        houseAreaAdapter.data.addAll(houseAreaEntity.filter { it.dong ==1 })
                        houseAreaAdapter.notifyDataSetChanged()
                    }
                }
                R.id.radio_two->{
                    mViewModel.mHouseSuccess.observe(this){
                        houseAreaAdapter.data.clear()
                        houseAreaEntity.clear()
                        val map = it.groupBy { it.area }
                        val list = map.keys.sorted()
                        var index = 1
                        for (key in list){
                            houseAreaEntity.add(HouseAreaEntity(index,key,map.get(key)!!))
                            index++
                        }
                        houseAreaAdapter.data.addAll(houseAreaEntity.filter { it.dong ==2 })
                        houseAreaAdapter.notifyDataSetChanged()
                    }
                }
                R.id.radio_three->{
                    mViewModel.mHouseSuccess.observe(this){
                        houseAreaAdapter.data.clear()
                        houseAreaEntity.clear()
                        val map = it.groupBy { it.area }
                        val list = map.keys.sorted()
                        var index = 1
                        for (key in list){
                            houseAreaEntity.add(HouseAreaEntity(index,key,map.get(key)!!))
                            index++
                        }
                        houseAreaAdapter.data.addAll(houseAreaEntity.filter { it.dong ==3 })
                        houseAreaAdapter.notifyDataSetChanged()
                    }
                }
            }
        }


        //点击租户管理跳转到 租户管理页面
        mDataBinding.tv3.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_TENANT_PATH).navigation()
        }


    }


    override fun loadData() {
        super.loadData()
        mViewModel.getHouseData(1)
        mViewModel.mHouseSuccess.observe(this){
            houseAreaAdapter.data.clear()
            houseAreaEntity.clear()
            val map = it.groupBy { it.area }
            val list = map.keys.sorted()
            var index = 1
            for (key in list){
                houseAreaEntity.add(HouseAreaEntity(index,key,map.get(key)!!))
                index++
            }
            houseAreaAdapter.data.addAll(houseAreaEntity)
            houseAreaAdapter.notifyDataSetChanged()
            Log.d("--->aaa","${it}")
        }

    }

    override fun observeDataChanged() {
        super.observeDataChanged()

    }

    override fun onResume() {
        super.onResume()
        mViewModel.getHouseData(1)
    }

}